<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/11
 * Time: 10:37
 */

namespace app\data;

use app\common\Common;
use think\Db;

class AssociationData extends Base
{
    /**
     * 添加商会
     * @param $association_name
     * @param $association_address
     * @param $association_introduce
     * @param $tel
     * @param $city_id
     * @param $type_id
     * @param $association_logo
     * @param $association_bg
     * @param $group_img
     * @param $legal_id_img
     * @param $other_img
     * @return array
     */
    public function add($association_name, $association_address, $association_introduce,
                        $tel, $city_id, $type_id, $association_logo, $association_bg, $group_img, $legal_id_img, $other_img)
    {
        $time = time();
        Db::startTrans();
        try {
            $id = Db::table('sj_association')
                ->insert([
                    'association_name' => $association_name,
                    'association_logo' => $association_logo,
                    'association_bg' => $association_bg,
                    'association_tel' => $tel,
                    'city_id' => $city_id,
                    'association_address' => $association_address,
                    'association_introduce' => $association_introduce,
                    'type_id' => $type_id,
                    'create_time' => $time
                ]);
            $this->addImg($id, $group_img, $legal_id_img, $other_img);
            Db::commit();
            return ['code' => $this->t_code, 'msg' => '添加成功'];
        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => $this->f_code, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 添加商会备案图片
     * @param $id
     * @param $group_img
     * @param $legal_id_img
     * @param $other_img
     * @return bool
     */
    public function addImg($id, $group_img, $legal_id_img, $other_img)
    {
        Db::table('sj_association_record_img')
            ->insert([
                'association_id' => $id,
                'group_img' => $group_img,
                'legal_id_img' => $legal_id_img,
                'other_img' => $other_img
            ]);
        return true;
    }

    /**
     * 修改商会
     * @param $id
     * @param $association_name
     * @param $association_address
     * @param $association_introduce
     * @param $hot_lv
     * @param $recommend_lv
     * @param $tel
     * @param $city_id
     * @param $type_id
     * @param $association_logo
     * @param $association_bg
     * @param $group_img
     * @param $legal_id_img
     * @param $other_img
     * @return array
     */
    public function edit($id, $association_name, $association_address, $association_introduce,
                         $tel, $city_id, $type_id, $association_logo, $association_bg, $group_img, $legal_id_img, $other_img)
    {
        Db::startTrans();
        try {
            $id = Db::table('sj_association')
                ->where([
                    'id' => $id
                ])
                ->update([
                    'association_name' => $association_name,
                    'association_logo' => $association_logo,
                    'association_bg' => $association_bg,
                    'association_tel' => $tel,
                    'city_id' => $city_id,
                    'association_address' => $association_address,
                    'association_introduce' => $association_introduce,
                    'type_id' => $type_id,
                    'verify_status' => 0
                ]);
            $this->editImg($id, $group_img, $legal_id_img, $other_img);
            Db::commit();
            return ['code' => $this->t_code, 'msg' => '修改成功'];
        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => $this->f_code, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 修改商户备案图片
     * @param $id
     * @param $group_img
     * @param $legal_id_img
     * @param $other_img
     * @return bool
     */
    public function editImg($id, $group_img, $legal_id_img, $other_img)
    {
        Db::table('sj_association_record_img')
            ->where([
                'association_id' => $id,
            ])
            ->update([
                'group_img' => $group_img,
                'legal_id_img' => $legal_id_img,
                'other_img' => $other_img
            ]);
        return true;
    }

    /**
     * 修改商户审核状态
     * @param $id
     * @param $status 1--OK  2--pass
     * @return array
     * @throws \think\Exception
     */
    public function editVerify($id, $status)
    {
        Db::table('sj_association')
            ->where([
                'id' => $id
            ])->update([
                'verify_status' => $status
            ]);
        Common::changeSession('association', $status, $id);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    /**
     * 删除商会
     * @param $id
     * @return array
     */
    public function del($id)
    {
        Db::startTrans();
        try {
            Db::table('sj_association')
                ->where([
                    'id' => $id,
                    'recover_status' => 1
                ])->delete();
            $this->delImg($id);
            Db::commit();
            return ['code' => $this->t_code, 'msg' => '删除成功'];
        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => $this->f_code, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 假删除商会
     * @param $id
     * @return array
     * @throws \think\Exception
     */
    public function fakeDel($id)
    {
        Db::table('sj_association')
            ->where([
                'id' => $id,
            ])->update([
                'recover_status' => 1
            ]);
        Common::changeSession('association', 2, $id);
        return ['code' => $this->t_code, 'msg' => '删除成功'];
    }

    /**
     * 删除图片
     * @param $id
     * @return bool
     * @throws \think\Exception
     */
    public function delImg($id)
    {
        Db::table('sj_association_record_img')
            ->where([
                'shop_id' => $id
            ])->delete();
        return true;
    }

    /**
     * 商户展示
     * @param $page
     * @param $pre
     * @param $sort
     * @param $recover_status
     * @param $verify_status
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function showList($page, $pre, $sort, $recover_status = 0, $verify_status = 1)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('sj_association a')
            ->join('sj_city b', 'a.city_id=b.id', 'left')
            ->field([
                'a.id',
                'a.association_name',
                "case a.verify_status when 0 then '待审核' when 1 then '审核通过' else '审核失败' end verify ",
                'a.association_logo',
                'a.association_address',
                'b.city_name',
                "FROM_UNIXTIME(a.create_time,'%Y-%m-%d %H:%i:%s') time"
            ])->where([
                'a.recover_status' => $recover_status,
                'a.verify_status' => $verify_status
            ])
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    /**
     * 获取企业数量
     * @param $where
     * @param $table_name
     * @return mixed
     */
    public function total($where, $table_name = 'sj_shop')
    {
        $res = Db::table($table_name)
            ->field('count(*) count')
            ->where($where)
            ->find();
        return $res['count'];
    }

    /**
     * 点亮图片
     * @param $id
     * @param $icon_id
     * @return array
     */
    public function addIcon($id, $icon_id)
    {
        Db::table('sj_association_icon')
            ->insert([
                'icon_id' => $icon_id,
                'shop_id' => $id,
                'type_id' => 1,
            ]);
        return ['code' => $this->t_code, 'msg' => '开通成功'];
    }

    /**
     * 取消点亮图标
     * @param $id
     * @param $shop_id
     * @return array
     * @throws \think\Exception
     */
    public function delIcon($id, $shop_id)
    {
        Db::table('sj_association_icon')
            ->where([
                'icon_id' => $id,
                'shop_id' => $shop_id,
                'type_id' => 1,
            ])->delete();
        return ['code' => $this->t_code, 'msg' => '取消成功'];
    }

    /**
     * 添加组织架构
     * @param $association_id
     * @param $association_position
     * @param $position_user_name
     * @param $shop_name
     * @param $shop_position_user_name
     * @param $sort_lv
     * @param $user_header
     * @return array
     */
    public function addGroupStructure($association_id, $association_position, $position_user_name, $shop_name,
                                      $shop_position_user_name, $sort_lv, $user_header)
    {
        $time = time();
        Db::table('sj_group_structure')
            ->insert([
                'association_id' => $association_id,
                'association_position' => $association_position,
                'position_user_name' => $position_user_name,
                'user_header' => $user_header,
                'shop_name' => $shop_name,
                'shop_position_user_name' => $shop_position_user_name,
                'sort_lv' => $sort_lv,
                'create_time' => $time
            ]);
        return ['code' => $this->t_code, 'msg' => '添加成功'];
    }

    /**
     * 修改组织架构
     * @param $id
     * @param $association_position
     * @param $position_user_name
     * @param $shop_name
     * @param $shop_position_user_name
     * @param $sort_lv
     * @param $user_header
     * @return array
     */
    public function editGroupStructure($id, $association_position, $position_user_name, $shop_name,
                                       $shop_position_user_name, $sort_lv, $user_header)
    {
        Db::table('sj_group_structure')
            ->where([
                'id' => $id
            ])
            ->update([
                'association_position' => $association_position,
                'position_user_name' => $position_user_name,
                'user_header' => $user_header,
                'shop_name' => $shop_name,
                'shop_position_user_name' => $shop_position_user_name,
                'sort_lv' => $sort_lv,
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    /**
     * 假删除组织构架
     * @param $id
     * @return array
     * @throws \think\Exception
     */
    public function fakeGroupStructureDel($id)
    {
        Db::table('sj_group_structure')
            ->where([
                'id' => $id
            ])->update([
                'recover_status' => 1
            ]);
        return ['code' => $this->t_code, 'msg' => '删除成功'];
    }

    /**
     * 组织构架显示
     * @param $page
     * @param $pre
     * @param $sort
     * @param int $recover_status |  是否删除  0--不删除  1--删除
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function showGroupStructure($page, $pre, $sort, $recover_status = 0)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);
        return Db::table('sj_group_structure')
            ->field([
                'user_header',
                'association_position',
                'position_user_name',
                'shop_name',
                'shop_position_user_name',
            ])->where([
                'recover_status' => $recover_status
            ])
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    /**
     * 修改审核状态
     * @param $id
     * @param $status
     * @return array
     * @throws \think\Exception
     */
    public function editAssociationVerify($id, $status)
    {
        Db::table('sj_association_shop')
            ->where([
                'id' => $id
            ])->update([
                'verify_status' => $status
            ]);
        return ['code' => $this->t_code, 'msg' => '审核状态修改成功'];
    }

    /**
     * 修改商会下的商户信息
     * @param $id
     * @param $type_name
     * @param $sort_lv
     * @return array
     * @throws \think\Exception
     */
    public function editShopInfo($id, $type_name, $sort_lv)
    {
        Db::table('sj_association_shop')
            ->where([
                'id' => $id
            ])->update([
                'type_name' => $type_name,
                'sort_lv' => $sort_lv
            ]);
        return ['code' => $this->t_code, 'msg' => '修改信息成功'];
    }

    /**
     * 商会下会员展示
     * @param $page
     * @param $pre
     * @param $sort
     * @param int $recover_status |   删除状态 0--未删除   1--删除
     * @param int $verify_status |   审核状态 0--待审核  1--审核通过  2--审核不通过
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function showMemberUnit($page, $pre, $sort, $recover_status = 0, $verify_status = 1)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('sj_association_shop a')
            ->join('sj_shop b', 'a.shop_id=b.id', 'left')
            ->join('sj_industry c', 'b.industry_id=c.id', 'left')
            ->field([
                'a.id',
                'b.shop_logo',
                'b.shop_name',
                'c.industry_name',
                'a.type_name'
            ])->where([
                'a.recover_status' => $recover_status,
                'a.verify_status' => $verify_status
            ])
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    /**
     * 删除商会会员.假
     * @param $id
     * @param $association_id
     * @return array
     * @throws \think\Exception
     */
    public function fakeMemberUnitDel($id, $association_id)
    {
        Db::table('sj_association_shop')
            ->where([
                'id' => $id,
                'association_id' => $association_id
            ])->update([
                'recover_status' => 1
            ]);
        return ['code' => $this->t_code, 'msg' => '删除成功'];
    }

    /**
     * 显示对应id的商会
     * @param $association_id
     * @param int $verify_status
     * @param int $recover_status
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function showAssociation($association_id, $verify_status = 1, $recover_status = 0)
    {
        return Db::table('sj_association a')
            ->join('sj_city b', 'a.city_id=b.id', 'left')
            ->field([
                'a.association_logo',
                'a.association_name',
                'b.city_name',
                'a.association_address'
            ])->where([
                'a.id' => ['in', $association_id],
                'a.verify_status' => $verify_status,
                'a.recover_status' => $recover_status
            ])->order('type_id desc')
            ->select();
    }

    /**
     * 搜索商会
     * @param $name
     * @param $page
     * @param $pre
     * @param $sort
     * @param int $recover_status
     * @param int $verify_status
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function searchAssociation($name, $page, $pre, $sort, $recover_status = 0, $verify_status = 1)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        $sort = ' a.type_id desc,' . $sort;
        return Db::table('sj_association a')
            ->join('sj_city b', 'a.city_id=b.id', 'left')
            ->field([
                'a.association_logo',
                'a.association_name',
                'b.city_name',
                'a.association_address'
            ])->where([
                'a.verify_status' => $verify_status,
                'a.recover_status' => $recover_status,
                'a.association_name' => ['like', "%{$name}%"]
            ])
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    /**
     * 显示商会的基本信息
     * @param $id
     * @param $verify_status
     * @param $recover_status
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function showAssociationInfo($id, $verify_status, $recover_status)
    {
        return Db::table('sj_association a')
            ->join('sj_city b', 'a.city_id=b.id', 'left')
            ->join('sj_province c', 'b.id=c.city_id', 'left')
            ->field([
                'a.id',
                'a.association_bg',
                'a.association_logo',
                'a.association_introduce',
                'c.province_name',
                'b.city_name',
                'a.association_address',
                'a.association_tel',
            ])->where([
                'a.verify_status' => $verify_status,
                'a.recover_status' => $recover_status,
                'a.id' => $id
            ])->find();
    }

    public function getAssociationIcon($id)
    {
        return Db::table('sj_association_icon a')
            ->join('sj_icon_config b', 'a.icon_id=b.id', 'left')
            ->field([
                'b.id',
                'b.icon_name',
                'b.img'
            ])->where([
                'a.shop_id' => $id,
                'a.type_id' => 1
            ])->select();
    }

    /**
     * 获取所有的图标
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getIcon()
    {
        return Db::table('sj_icon_config')
            ->field([
                'id',
                'icon_name',
                'img_gray img'
            ])->select();
    }

    /**
     * 添加商会轮播图
     * @param $id
     * @param $img
     * @return array
     */
    public function addCarouselImg($id, $img)
    {
        $data = [];
        $time = time();
        foreach ($img as $k => $v) {
            $data[$k]['img'] = $v;
            $data[$k]['shop_id'] = $id;
            $data[$k]['type_id'] = 1;
            $data[$k]['create_time'] = $time;
        }
        Db::table('sj_carousel_img')
            ->insert($data);
        return ['code' => $this->t_code, 'msg' => '添加成功'];
    }

    /**
     * 更新录播图
     * @param $id
     * @param $img
     * @return array
     * @throws \think\Exception
     */
    public function editCarouselImg($id, $img)
    {
        $data = [];
        $time = time();
        foreach ($img as $k => $v) {
            $data[$k]['img'] = $v;
            $data[$k]['shop_id'] = $id;
            $data[$k]['type_id'] = 1;
            $data[$k]['create_time'] = $time;
        }
        Db::startTrans();
        try {
            $this->delCarouselImg($id);
            Db::table('sj_carousel_img')
                ->insert($data);
            Db::commit();
            return ['code' => $this->t_code, 'msg' => '添加成功'];
        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => $this->t_code, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 删除轮播图
     * @param $id
     * @return bool
     * @throws \think\Exception
     */
    private function delCarouselImg($id)
    {
        Db::table('sj_carousel_img')
            ->where([
                'shop_id' => $id,
                'type_id' => 1
            ])->delete();
        return true;
    }

    /**
     * 获取求购信息
     * @param $page
     * @param $pre
     * @param $sort
     * @param $id
     * @param int $recover_status
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function showShopBuying($id, $page, $pre, $sort, $recover_status = 0)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('sj_shop_buying a')
            ->join('sj_shop b', 'a.shop_id=b.id', 'left')
            ->join('sj_association_shop c','a.shop_id=c.shop_id','left')
            ->field([
                'a.id',
                'b.shop_logo',
                'a.contacts_name',
                'a.contacts_tel',
                'b.shop_name',
                'a.shop_id',
                'a.content',
                'a.quantity',
                "FROM_UNIXTIME(a.create_time,'%Y-%m-%d') start_time",
                "FROM_UNIXTIME(a.end_time,'%Y-%m-%d') end_time",
            ])->where([
                'a.recover_status' => $recover_status,
                'c.association_id'=>$id
            ])
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    /**
     * 获取商品列表信息
     * @param $page
     * @param $pre
     * @param $sort
     * @param $id
     * @param int $recover_status
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function showGoodsList($id, $page, $pre, $sort, $recover_status = 0)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);
        return Db::table('sj_shop_goods a')
            ->join('sj_association_shop b','a.shop_id=b.shop_id','left')
            ->field([
                'a.id',
                'a.shop_id',
                'a.goods_name',
                'a.goods_img'
            ])->where([
                'a.recover_status' => $recover_status,
                'b.association_id'=>$id
            ])
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    /**
     * 获取商品详细信息
     * @param $id
     * @return $this
     */
    public function showGoodsInfo($id)
    {
        return Db::table('sj_shop_goods')
            ->field([
                'id',
                'shop_id',
                'goods_name',
                'goods_img',
                'goods_description'
            ])->where([
                'id' => $id
            ])->find();
    }
}